import java.util.Stack;

/**
 * @author SaoE
 * @date 2025/3/22 15:58
 */
public class _234 {
    public boolean isPalindrome(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        int length = 0;
        ListNode cur = head;
        while (cur != null){
            cur = cur.next;
            length++;
        }
        if (length % 2 == 0){
            for (int i = 0; i < length / 2; i++){
                stack.push(head.val);
                head = head.next;
            }
            while (head != null){
                if (head.val != stack.pop()){
                    return false;
                }
                head = head.next;
            }
        }else {
            for (int i = 0; i < length / 2; i++){
                stack.push(head.val);
                head = head.next;
            }
            head = head.next;
            while (head != null){
                if (head.val != stack.pop()){
                    return false;
                }
                head = head.next;
            }
        }
        return true;
    }
}
